﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WIKID.Controller;
using WIKID.Common;
using WIKID.Model;

namespace WIKID.View
{
    public partial class ScheduleView : UserControl
    {
        private ParentController parentController;

        private AccountModel accountModel;

        public ScheduleView()
        {
            InitializeComponent();
            parentController = new ParentController();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ScheduleView_Load(object sender, EventArgs e)
        {
            if (!DesignMode)
            {
                BindAccounts();
                BindSubjects();
            }
        }

        /// <summary>
        /// Method to bind Accounts to combobox
        /// </summary>
        private void BindAccounts()
        {
            Dictionary<int, string> dicAccounts = parentController.ListingAccounts();
            //cbAccounts.Items.Add(Constants.THERE_NO_ACCOUNT);
            if (dicAccounts.Count != 0)
            {
                cbAccounts.DataSource = new BindingSource(dicAccounts, null);
                cbAccounts.DisplayMember = "Value";
                cbAccounts.ValueMember = "Key";
            }
            //cbAccounts.SelectedItem = cbAccounts.Items[0];
        }

        /// <summary>
        /// Method to bind Subjects to combobox
        /// </summary>
        private void BindSubjects()
        {
            Dictionary<int, string> dicSubjects = parentController.ListingSubjects();
            //cbSubjects.Items.Add(Constants.THERE_NO_SUBJECT);
            if (dicSubjects.Count != 0)
            {
                cbSubjects.DataSource = new BindingSource(dicSubjects, null);
                cbSubjects.DisplayMember = "Value";
                cbSubjects.ValueMember = "Key";
            }
            //cbSubjects.SelectedItem = cbSubjects.Items[0];
        }

        /// <summary>
        /// Method to bind Lectures to combobox
        /// </summary>
        private void BindLectures(KeyValuePair<int,string> subject)
        {
            Dictionary<int, string> dicLectures = parentController.ListingLectures(subject);
            
            //cbLectures.Items.Add(Constants.THERE_NO_LECTURE);
            if (dicLectures.Count != 0)
            {
                cbLectures.DataSource = new BindingSource(dicLectures, null);
                cbLectures.DisplayMember = "Value";
                cbLectures.ValueMember = "Key";
            }
            //cbLectures.SelectedItem = cbLectures.Items[0];
        }

        private void cbLectures_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Method handle event Click of button Set
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSet_Click(object sender, EventArgs e)
        {
            ScheduleModel scheduleModel = new ScheduleModel();
            scheduleModel.LectureId = cbLectures.SelectedValue.ToString();
            scheduleModel.StartDateTime = dtpStartDate.Value;
            scheduleModel.EndDateTime = dtpEndDate.Value;
            if (parentController.UpdateSchedule(accountModel, scheduleModel))
            {
                MessageBox.Show("Set lecture's schedule successfully!", "Succeed!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else 
            {
                MessageBox.Show("Fail on setting lecture's schedule!", "Failure!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbAccounts_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbAccounts.SelectedValue != null && !cbAccounts.SelectedValue.ToString().Equals(Constants.THERE_NO_ACCOUNT))
            {
                KeyValuePair<int, string> account = (KeyValuePair<int, string>)cbAccounts.SelectedItem;
                string fileName = account.Key + Constants.SEPARATE_CHAR + account.Value;
                accountModel = parentController.GetAccountModel(fileName);
                picAccount.Image = Image.FromFile(accountModel.AccPic);
                lblAccountName.Text = accountModel.AccName;
                btnSet.Enabled = true;
            }
            else
            {
                btnSet.Enabled = false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbSubjects_SelectedIndexChanged(object sender, EventArgs e)
        {
            //cbLectures.DataSource = new BindingSource();

            if (cbSubjects.SelectedItem != null && !cbSubjects.SelectedItem.ToString().Equals(Constants.THERE_NO_SUBJECT))
            {
                KeyValuePair<int, string> subject = new KeyValuePair<int, string>();
                subject = (KeyValuePair<int, string>)cbSubjects.SelectedItem;
                BindLectures(subject);
            }
        }
    }
}
